package cn.wolfcode.wolf2w.web.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domain.DestinationEs;
import cn.wolfcode.wolf2w.search.domain.StrategyEs;
import cn.wolfcode.wolf2w.search.domain.TravelEs;
import cn.wolfcode.wolf2w.search.domain.UserInfoEs;
import cn.wolfcode.wolf2w.search.qo.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.ISearchService;
import cn.wolfcode.wolf2w.search.vo.SearchResultVO;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.uitl.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearshController {
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private ISearchService searchService;

    //分业务的地方
    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
       String str = URLDecoder.decode(qo.getKeyword(),"UTF-8");
        qo.setKeyword(str);//改编码
        switch (qo.getType()){
            case  SearchQueryObject.TYPE_DEST :
                //目的地
                return searchDest(qo);
            case  SearchQueryObject.TYPE_STRATEGY :
                //攻略
                return searchStrategy(qo);
            case  SearchQueryObject.TYPE_TRAVEL :
                //游记
                return searchTravel(qo);
            case  SearchQueryObject.TYPE_USER :
                //用户
                return searchUser(qo);
            default:
                //全部
                return searchAll(qo);
        }
    }


    //查询所有数据
    private Object searchAll(SearchQueryObject qo) {
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subTitle", "summary");
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title", "summary");
        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "city", "info");
        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo,
                "name", "info");
        SearchResultVO vo = new SearchResultVO();
        //拿他里面所有类
        vo.setDests(ds.getContent());
        vo.setStrategys(sts.getContent());
        vo.setTravels(ts.getContent());
        vo.setUsers(us.getContent());
        //拿到总长度
        vo.setTotal(sts.getTotalElements() + ts.getNumberOfElements() + us.getTotalElements() + ds.getTotalElements());
        //创建一个map
        HashMap<String, Object> map = new HashMap<>();
        map.put("result",vo);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    //查询用户数据
    private Object searchUser(SearchQueryObject qo) {
        Page<UserInfo> page = searchService.searchWithHighlight
                (UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class,
                        qo, "info", "city");

        //创建一个map
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    //查询游记
    private Object searchTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight
                (TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class,
                        qo, "title", "summary");

        //创建一个map
        Map<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    //查询攻略
    private Object searchStrategy(SearchQueryObject qo) {
        Page<Strategy> page = searchService.searchWithHighlight
                (StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class,
                        qo, "title", "subTitle", "summary");

        //创建一个map
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    //查询目的地,这个是精确查询
    private Object searchDest(SearchQueryObject qo) {

        Destination des =destinationService.queryfindByName(qo.getKeyword());
        //通过目的地查询他下面的所有攻略游记和用户
        //创建一个vo的类
        SearchResultVO vo = new SearchResultVO();
        if (des !=null){
            List<Strategy > strategy=  strategyService.queryfindByDestName(qo.getKeyword());
            List<Travel> travel=  travelService.queryfindByDestName(qo.getKeyword());
            List<UserInfo> userInfo=  userInfoService.queryfindByCity(qo.getKeyword());
            //添加在vo中
            vo.setStrategys(strategy);
            vo.setTravels(travel);
            vo.setUsers(userInfo);
            vo.setTotal((long)strategy.size()+travel.size()+userInfo.size());
        }
        //创建一个map
        HashMap<String, Object> map = new HashMap<>();
        map.put("result",vo);
        map.put("qo",qo);
        map.put("dest",des);
        return JsonResult.success(map);
    }
}
